﻿using System.Windows;
using Microsoft.Wpf.Samples.BaseLibrary;
using System;

namespace FollowTheMouseWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        double speed;
        double distance;

        DateTime lastTime = DateTime.Now;
        Point lastPos;

        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += (s, e) =>
                {
                    lastTime = DateTime.Now;

                    GlassEffectHelper.EnableGlassEffect(this);

                    MouseIntercept.SetHook();

                    MouseIntercept.MousePositionChanged += (s1, e1) =>
                        {
                            #region Calculate Mouse Distance

                            Point pos = MouseIntercept.MousePosition;
                            double diffDistance = 0;

                            if (lastPos != null)
                            {
                                diffDistance = Math.Sqrt(lastPos.X * lastPos.X + lastPos.Y * lastPos.Y);
                                distance += diffDistance;
                            }
                            lastPos = pos;

                            TimeSpan ts = DateTime.Now.Subtract(lastTime);

                            speed = diffDistance / ts.Milliseconds;
                            _speed.Text = (int)speed + " pixels / second" + Environment.NewLine
                                + (int)distance + " pixels total";
                            
                            _mouseposition.Text = pos.X + " - " + pos.Y;

                            lastTime = DateTime.Now;

                            #endregion

                            leftEye.SetMouse(pos);
                            rightEye.SetMouse(pos);
                        };
                };
        }

        ~MainWindow()
        {
            MouseIntercept.Unhook();
        }
    }
}
